<html>
<head><meta charset="utf-8"><title>CARGO_CFG_TEST · t-cargo · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/index.html">t-cargo</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html">CARGO_CFG_TEST</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211381644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211381644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211381644">(Sep 26 2020 at 20:37)</a>:</h4>
<p>Hi, is there an alternative to detect that a build script is run to build a version of the lib used as a test? According to the documentation, the environment variable <code>CARGO_CFG_TEST</code> should be set, but it's not the case. I found this old issue about it: <a href="https://github.com/rust-lang/cargo/issues/4789">https://github.com/rust-lang/cargo/issues/4789</a></p>



<a name="211389870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211389870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211389870">(Sep 27 2020 at 00:39)</a>:</h4>
<p><span class="user-mention" data-user-id="325787">@Émile Grégoire</span> The build script runs for the package as a whole, it is not possible to determine which command is being used (build, test, check, etc.).  Where in the documentation does it say that <code>CARGO_CFG_TEST</code> should be set?</p>



<a name="211390358"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390358" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390358">(Sep 27 2020 at 00:55)</a>:</h4>
<p><span class="user-mention" data-user-id="120518">@Eric Huss</span> It's written <a href="https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts">here</a> that a <code>CARGO_CFG_&lt;cfg&gt;</code> is generated for each configuration option of the package being built, and it's written <a href="https://doc.rust-lang.org/reference/conditional-compilation.html#test">here</a> that <code>test</code> is such an option. So I thought it would be set.</p>



<a name="211390403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390403">(Sep 27 2020 at 00:56)</a>:</h4>
<p>I see.  That should definitely be clearer about what gets set.</p>



<a name="211390409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390409">(Sep 27 2020 at 00:56)</a>:</h4>
<p>So I don't have any option to modify stuff in my build script if it's being built for tests?</p>



<a name="211390420"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390420" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390420">(Sep 27 2020 at 00:57)</a>:</h4>
<p>not really.  you could (manually) set a feature when running tests.</p>



<a name="211390486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390486">(Sep 27 2020 at 00:59)</a>:</h4>
<p>I'm creating a crate that provides a FFI to a C++ library. I write a C FFI in C++ that I compile with <code>cc</code>. The C++ library in question is not open-source, I only have the headers and a compiled .lib (it's Windows only). And that .lib relies on symbols defined in an executable (the executable loads the DLL that is written in Rust). My goal was to have a C++ macro to stub everything when testing and not link with the .lib, so users could write tests without having undefined symbols.</p>



<a name="211390530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390530">(Sep 27 2020 at 01:00)</a>:</h4>
<p>It's a weird use-case tbh</p>



<a name="211390554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390554">(Sep 27 2020 at 01:01)</a>:</h4>
<p>I haven't thought about having a feature though. I could filter by <code>CARGO_FEATURE_&lt;name&gt;</code> then... I might try that one.</p>



<a name="211390747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211390747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Émile Grégoire <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211390747">(Sep 27 2020 at 01:06)</a>:</h4>
<p>I could set it in <code>required-features</code> of <code>[[test]]</code> in the manifest, right? Instead of providing it in the command line manually.</p>



<a name="211394144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/CARGO_CFG_TEST/near/211394144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/CARGO_CFG_TEST.html#211394144">(Sep 27 2020 at 02:41)</a>:</h4>
<p><code>required-features</code> will just cause the test to be skipped if the feature isn't set. You still have to pass the feature on the command-line to enable it.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>